JAVA 使用HTML2IMAGE将HTML转图片 您所在的位置:网站首页 html2image 中文乱码 JAVA 使用HTML2IMAGE将HTML转图片

JAVA 使用HTML2IMAGE将HTML转图片

2023-11-06 15:22| 来源: 网络整理| 查看: 265

需要的jar 第二个jar是工具类是用于日期转换的 第一个才是Html2Image所需要的包

gui.ava html2image 0.9 cn.hutool hutool-all 5.0.6

  

转换代码

HtmlImageGenerator imageGenerator = new HtmlImageGenerator(); String htmlstr = "\n" + " \n" + " 收据\n" + " \n" + " \n" + " 日期\n" + " "+DateUtil.format(saleOrder.getCreateTime(), "yyyy-MM-dd HH:mm:ss")+"\n" + " \n" + " \n" + " 交易编号\n" + " "+saleOrder.getOrderCode()+"\n" + " \n" + " \n" + " 交易类型\n" + " 捐赠\n" + " \n" + " \n" + " 交易金额\n" + " "+saleOrder.getProductPrice()+"\n" + " \n" + " \n" + " 付款人\n" + " "+saleOrder.getUserName()+"\n" + " " + ""; imageGenerator.loadHtml(htmlstr); BufferedImage bufferedImage = getGrayPicture(imageGenerator.getBufferedImage()); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); try { ImageIO.write(bufferedImage, "jpg", outputStream); String base64Img = Base64.encodeBase64String(outputStream.toByteArray()); String res = "data:image/jpg;base64," + base64Img.toString(); modelAndView.addObject("imageres", res); } catch (IOException e) { e.printStackTrace(); }finally { if(outputStream != null){ try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } }

  

这个是用于重新设置画布背景颜色的 

 

public BufferedImage getGrayPicture(BufferedImage originalImage) { BufferedImage grayPicture; int imageWidth = originalImage.getWidth(); int imageHeight = originalImage.getHeight(); grayPicture = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB); ColorConvertOp cco = new ColorConvertOp(ColorSpace .getInstance(ColorSpace.CS_GRAY), null); cco.filter(originalImage, grayPicture); return grayPicture; }

  

前端:

效果:

 

 

loadUrl(url) (从url载入html) loadHtml(html) (载入本地html) saveAsImage(file) (以图片形式保存html) saveAsHtmlWithMap(file, imageUrl) (创建一个HTML文件包含客户端image-map) getLinks()(列出所有在HTML文档的链接和相应href、目标、头衔、位置和尺寸) getBufferedImage() (获得awt,html缓冲后的图片) getLinksMapMarkup(mapName) (HTML代码段里获得的客户端image-map 产生的链接) get/setOrientation(orientation) (get/set文本定位) get/setSize(dimension) (设置生成图片大小)

  

参考文章

https://blog.csdn.net/luohaobubu/article/details/7414554?utm_source=blogxgwz5

https://blog.csdn.net/fykhlp/article/details/6204714

 

原文地址:https://www.freesion.com/article/6136419823/

 



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有